package com.ethereal.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class Login extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if ("admin".equals(username) && "123456".equals(password)) {
            // 1. 请求转发（forward）到 welcome.jsp，URL 不变
            req.setAttribute("username", username);
            req.getRequestDispatcher("index.jsp").forward(req, resp);

            // 2. 或者重定向（redirect）到另一个 Servlet，URL 改变
            // response.sendRedirect("showData");
        } else {
            // 登录失败，返回错误信息
            req.setAttribute("error", "用户名或密码错误");
            req.getRequestDispatcher("99Table.jsp").forward(req, resp);
        }
    }
}
